<?php

namespace app\admin\controller;

use think\Db;
use think\Session;

class Seo extends Authorized
{

    public function index(){
        $seo=Db::table('tb_admin')->where(['departid'=>3,'state'=>0])->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }//所有站点
    public function my(){return $this->fetch();} //优化师个人站点
    public function qf(){
        $seo=Db::table('tb_admin')->where(['departid'=>3,'state'=>0])->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }

    public function stop(){
        $data=['departid'=>3,'state'=>0];

        if(Session::get('admin.levelNum') == 1){
            $data['id']=Session::get('admin.id');
        }
        $seo=Db::table('tb_admin')->where($data)->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }
//   可申请站点
    public function soseo(){
        $seo=Db::table('tb_admin')->where(['departid'=>3,'state'=>0])->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }
    public function getList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $type=input('type');
        $keywords = input('keywords');
        $seoid = input('seoid');
        $where=[];
        $order='';

        if(Session::get('admin.levelNum')==1){
            $where['w.seoid']=Session::get('admin.id');
        }
        if (!empty($type)){
            if ($type=='zc'){
                $where['w.state']=1;
                $where['u.money']=['>=',0];
                $order='state,id desc';
            } //正常客户
            if ($type=='qf'){
                $where['w.state']=1;
                $where['u.money']=['<',0];
                $order='qftime desc,id desc';
            } //欠费未丢单客户
            if ($type=='stop'){
                $where['w.state']=3;
                $order='closetime desc,id desc';
            }//丢单客户
            if ($type=='noSeo'){
                $where['w.state']=1;
                $where['w.seoid']=0;
                $order='id desc';
            }//丢单客户

        }

        if (!empty($seoid)){
            $where['w.seoid']=$seoid;
        }
        if (!empty($keywords)){
            $where['u.name|w.url|w.urlName']=['like','%'.$keywords.'%'];
        }

        $count=Db::table('tb_webinfo')->alias('w')->field('w.*,a.name seo_name,u.money user_money,u.closetime')
            ->join('tb_admin a','a.id=w.seoid','left')
            ->join('tb_user u','u.id=w.userID','left')
            ->where($where)->count();

        $rows=Db::table('tb_webinfo')->alias('w')->field('w.*,a.name seo_name,u.money user_money,u.closetime')
            ->join('tb_admin a','a.id=w.seoid','left')
            ->join('tb_user u','u.id=w.userID','left')
            ->where($where)
            ->order($order)->page($page, $limit)->select();
        return json(['code' =>0,'count' =>$count,'msg' =>'','data'=>$rows]);
    }

//查看重复扣费 moneyRepeat
    public function moneyrepeat(){
        $get_data=$this->request->get();
        if(!empty($get_data['intime'])){
            $intime=$get_data['intime'];
        }else{
            $intime=date('Y-m-d',strtotime('-1 day'));
        }
        $this->assign('intime', $intime);

        return $this->fetch();
    }

    public function getMoneyRepeat()
    {
        $get_data=$this->request->get();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $intime = input('intime');
        $seoid = input('seoid');
        $url = input('url');
        $urlName = input('urlName');
        $keywords = input('keywords');
        $where=[];

        $where['keyid']=['>',0];


        if(!empty($intime)){
            $where['intime']=['between time',[$intime.' 00:00:00',$intime.' 23:59:59']];
        }else{
            $time=date('Y-m-d',strtotime('-1 day'));
            $where['intime']=['between time',[$time.' 00:00:00',$time.' 23:59:59']];
        }



//        Select keyid,Count(*) From [tb_Money] where datediff(d,intime,GETDATE())=1 and keyid>0 Group By keyid Having Count(*) > 1

        $rows=Db::table('tb_money')->field('keyid,name,count(id) num')->where($where)->group('keyid,name')
            ->having('count(id)>1')
            ->select();

        return json(['code' =>0,'msg' =>Db::table('tb_money')->getLastSql(),'data'=>$rows]);
    }



//金额返还页面 moneyReturn
    public function moneyreturn(){
        $get_data=$this->request->get();
        if(!empty($get_data['intime'])){
            $intime=$get_data['intime'];
        }else{
            $intime=date('Y-m-d',strtotime('-1 day'));
        }
        $this->assign('intime', $intime);

        if(!empty($get_data['seoid'])){
            $seoid=$get_data['seoid'];
            $this->assign('seoid', $seoid);
        }

        $seo=Db::table('tb_admin')->where(['departid'=>3,'state'=>0])->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }
//金额返还页面


//金额返还页面
    public function moneylist(){
        $get_data=$this->request->get();
        if(!empty($get_data['intime'])){
            $intime=$get_data['intime'];
        }else{
            $intime=date('Y-m-d',strtotime('-1 day'));
        }
        $this->assign('intime', $intime);

        if(!empty($get_data['seoid'])){
            $seoid=$get_data['seoid'];
            $this->assign('seoid', $seoid);
        }

        $seo=Db::table('tb_admin')->where(['departid'=>3,'state'=>0])->select();
        $this->assign('seo', $seo);
        return $this->fetch();
    }
    public function getMoneyList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $intime = input('intime');
        $seoid = input('seoid');
        $url = input('url');
        $urlName = input('urlName');
        $keywords = input('keywords');
        $where=[];

        $where['m.keyid']=['>',0];


        if (!empty($seoid)){
            $where['w.seoid']=$seoid;
        }
        if (!empty($urlName)){
            $where['w.urlName']=['like','%'.$urlName.'%'];
        }
        if (!empty($url)){
            $where['w.url']=['like','%'.$url.'%'];
        }
        if (!empty($keywords)){
            $where['m.Name|w.url|w.urlName']=['like','%'.$keywords.'%'];
        }

        if(!empty($intime)){
            $where['m.intime']=['between time',[$intime.' 00:00:00',$intime.' 23:59:59']];
        }else{
            $time=date('Y-m-d',strtotime('-1 day'));
            $where['m.intime']=['between time',[$time.' 00:00:00',$time.' 23:59:59']];
        }




        $count=Db::table('tb_money m')->field('m.*,a.name seo_name,w.url,w.urlName')
            ->join('tb_admin a','a.id=m.seoid','left')
            ->join('tb_webinfo w','w.id=m.webid','left')
            ->where($where)
            ->count();

        $rows=Db::table('tb_money m')->field('m.*,a.name seo_name,w.url,w.urlName')
            ->join('tb_admin a','a.id=m.seoid','left')
            ->join('tb_webinfo w','w.id=m.webid','left')
            ->where($where)
            ->order('webid')->page($page, $limit)->select();

        return json(['code' =>0,'count' =>$count,'msg' =>Db::table('tb_money')->getLastSql(),'data'=>$rows]);}

//金额返还页面


//优化师申请
    public function apply()
    {
        $post_data = $this->request->post();
        if($post_data){
            $seoid=Session::get('admin.id');
            $num=Db::table('tb_webinfoApply')->where(['webid'=>$post_data['webID'],'seoid'=>$seoid])->count();
            if($num>0){
                return json(['msg' =>'您已经申请过该网站了', 'code' =>1,'data'=>'']);
            }else{
                $result =Db::table('tb_webinfoApply')->insert(['webid'=>$post_data['webID'],'seoid'=>$seoid]);
                if (false === $result) {
                    return json(['msg' =>'申请失败', 'code' =>1,'data'=>'']);
                } else {
                    Db::table('tb_webinfo')->where('id',$post_data['webID'])->update(['ApplyNum'=>Db::raw('ApplyNum+1')]);
                    return json(['msg' =>'成功', 'code' =>0]);
                }
            }
        }


        $webID=input('webID');
        $article=Db::table('tb_webinfo')->where('id' ,$webID)->find();
        $this->assign('article', $article);

        $list=Db::table('tb_webinfoApply a')->field('a.*,b.name,b.nickname,b.webnum,b.webdbnum,b.keynum,b.keybdnum')
            ->join('tb_admin b','b.id=a.seoid','left')
            ->where('webid' ,$webID)
            ->order('intime desc')->select();
        foreach($list as $k=>$v){
            if ($v['webnum']>0){
                $list[$k]['data_web']=sprintf("%01.2f", ($v['webdbnum'] / $v['webnum']) * 100) . '%';
            }else{
                $list[$k]['data_web']='0%';
            }
            if ($v['keynum']>0){
                $list[$k]['data_key']=sprintf("%01.2f", ($v['keybdnum'] / $v['keynum']) * 100) . '%';
            }else{
                $list[$k]['data_key']='0%';
            }
        }
        $this->assign('list', $list);
        return $this->fetch();
    }


}